package de.uni.muenster.idw2.listener;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import de.uni.muenster.idw2.R;
import de.uni.muenster.idw2.data.BBBuilding;
import de.uni.muenster.idw2.utilities.GeoHelper;
import de.uni.muenster.idw2.view.RotatorImage;


public class RotationListener implements LocationListener{
	
	private Location start;
	private boolean sensorIsEnabled = false;
	private SensorManager mySensorManager;
	private final Context context;
	private SensorEventListener sensorEventListener;
	private List<Sensor> mySensors;
	private boolean sensorIsSleeping = false;
	private ArrayList<BBBuilding> bbBuildings;
	private ArrayList<RotatorImage> rotatorImages;
	private ArrayList<TextView> textViewsDistance;
	private ArrayList<ImageView> imageViewsBuilding;

	public RotationListener(ArrayList<BBBuilding> bbBuildings, ArrayList<RotatorImage> rotatorImages, Context context, ArrayList<TextView> textViewsDistance, ArrayList<ImageView> imageViewsBuilding)
	{
		this.bbBuildings = bbBuildings;
		this.rotatorImages = rotatorImages;
		this.context = context;
		this.textViewsDistance = textViewsDistance;
		this.imageViewsBuilding = imageViewsBuilding;
		
		
	}

	@Override
	public void onLocationChanged(Location location) {
		start = location;
		if(!sensorIsEnabled)
		{
			sensorIsEnabled = true;
			startSensor();
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}
	
	private void startSensor()
	{
		sensorEventListener = new SensorEventListener() 
		{
			public void onSensorChanged(SensorEvent event) {
				
				for(int i = 0; i < bbBuildings.size(); i++){
					Location loc = new Location(bbBuildings.get(i).getName());
					loc.setLatitude(bbBuildings.get(i).getLat());
					loc.setLongitude(bbBuildings.get(i).getLon());
					if(start != null && loc != null){
						int angle = GeoHelper.calculateAngle(start, loc);
						
						int rotation;
						
						if((angle - event.values[0]) <= 180 && (angle - event.values[0]) >= -180)
						{
							rotation = (int) (angle - event.values[0]);
						}
						else if((angle - event.values[0]) >= 180)
						{
							rotation = (int) -(360 - angle + event.values[0]);
						}
						else
						{
							rotation = (int) (360 - event.values[0] + angle);
						}
						int dist =(int) loc.distanceTo(start);
						
						String decText = "~" + dist + " m";
						
						if(textViewsDistance.get(i) != null)
						{
							textViewsDistance.get(i).setVisibility(View.VISIBLE);
							textViewsDistance.get(i).setText(decText);
						}
						
						if(rotatorImages.get(i) != null){
							rotatorImages.get(i).setImageResource(R.drawable.arrow);
							rotatorImages.get(i).setVisibility(View.VISIBLE);
							rotatorImages.get(i).rotate((rotation));
						}
					}
				}
			}
			
			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
			}
		};
		
		mySensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
		//"rate suitable for games" Alternative: SENSOR_DELAY_FASTEST
		mySensorManager.registerListener(sensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_GAME);
	}
	
	public void disableOrientation(){
		if(sensorIsEnabled && !sensorIsSleeping){
			mySensorManager.unregisterListener(sensorEventListener);
			sensorIsSleeping = true;
		}
	}
	
	public void enableOrientation(){
		if(sensorIsEnabled && sensorIsSleeping){
			mySensorManager.registerListener(sensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_FASTEST);
			sensorIsSleeping = false;
		}
	}
	
	
}
